package com.analysis.core;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class MethodAnalyzer extends MethodVisitor {
    private final MethodInfo methodInfo;

    public MethodAnalyzer(MethodInfo methodInfo) {
        super(Opcodes.ASM9);
        this.methodInfo = methodInfo;
    }

    @Override
    public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
        methodInfo.addAnnotation(descriptor);
        return new AnnotationVisitor(Opcodes.ASM9) {
            @Override
            public void visit(String name, Object value) {
                if ("value".equals(name) || "path".equals(name)) {
                    String[] paths = value instanceof String[] ? (String[]) value : new String[]{(String) value};
                    if (paths.length > 0) {
                        methodInfo.setApiPath(paths[0]);
                    }
                }
                super.visit(name, value);
            }
        };
    }
}
